package com.example.newproject_overseas.Screen

import androidx.compose.material.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.newproject_overseas.R
import com.example.newproject_overseas.ViewModel.ImageViewModel
import com.example.newproject_overseas.components.TopApp
import getPicture
import org.json.JSONObject

@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterialApi::class)
@Composable
fun OcrCardScreen(navController: NavHostController) {

    val context = LocalContext.current
    var imgImageViewModel = ViewModelProvider(context as ViewModelStoreOwner).get(ImageViewModel::class.java)

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color(0XffF5F4F2)),

        ) {

        TopApp(title = "Upload the front of the ID card", navController = navController)
        Spacer(modifier = Modifier.height(40.dp))
        Row() {
            //两个平行的图片
            Column(
                    modifier = Modifier
                        .weight(1f)
                        .clickable {
                            //拍照
                            getPicture(context,JSONObject(),0)
                        },
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,

                ) {

                    if (imgImageViewModel.idcardfront_ImageBS.value != null) {
                        Image(
                            bitmap = imgImageViewModel.idcardfront_ImageBS.value!!,
                            contentDescription = null,
                            modifier = Modifier
                                .size(width = 170.dp, height = 140.dp)
                        )
                    }else{
                        Image(
                            painter = painterResource(id = R.drawable.idcardfront),
                            contentDescription = null,
                            modifier = Modifier
                                .size(width = 170.dp, height = 140.dp)
                        )
                    }


                Text(
                    text = "ID card front",
                    textAlign = TextAlign.Center,
                    maxLines = 1,

                    )



            }
            Column(
                modifier = Modifier
                    .weight(1f)
                    .clickable {
                        //拍照
                        getPicture(context,JSONObject(),1)
                    },
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
            ) {
                if (imgImageViewModel.idcardback_ImageBS.value != null) {
                    Image(
                        bitmap = imgImageViewModel.idcardback_ImageBS.value!!,
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 170.dp, height = 140.dp)
                    )
                }else{
                    Image(
                        painter = painterResource(id = R.drawable.idcardback),
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 170.dp, height = 140.dp)

                    )
                }
                Text(
                    text = "ID card back",
                    textAlign = TextAlign.Center,
                    maxLines = 1,

                    )
            }
        }

        //一张单独一行的图片
        Row() {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .clickable {
                        //拍照
                        getPicture(context,JSONObject(),2)
                    },
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
            ) {
                if (imgImageViewModel.panCard_ImageBS.value != null) {
                    Image(
                        bitmap = imgImageViewModel.panCard_ImageBS.value!!,
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 170.dp, height = 140.dp)
                    )
                }else{
                    Image(
                        painter = painterResource(id = R.drawable.pancard),
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 170.dp, height = 140.dp)

                    )
                }

                Text(
                    text = "Pan Card",
                    textAlign = TextAlign.Center,

                    )
            }
        }
        Spacer(modifier = Modifier.height(60.dp))
        //底下三个 图片
        Row() {
            Row() {
                Column(
                    modifier = Modifier
                        .weight(1f),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,

                    ) {
                    Image(
                        painter = painterResource(id = R.drawable.idcardfront),
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 100.dp, height = 70.dp)
                    )
                    Text(
                        text = "Incomplete Photo",
                        textAlign = TextAlign.Center,
                        maxLines = 1, fontSize = 10.sp,

                        )

                }
                Column(
                    modifier = Modifier
                        .weight(1f),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,


                    ) {
                    Image(
                        painter = painterResource(id = R.drawable.idcardfront),
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 100.dp, height = 70.dp)
                    )
                    Text(
                        text = "Blurry Photo",
                        textAlign = TextAlign.Center,
                        maxLines = 1,
                        fontSize = 10.sp,

                        )
                }
                Column(
                    modifier = Modifier
                        .weight(1f),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,

                    ) {
                    Image(
                        painter = painterResource(id = R.drawable.idcardfront),
                        contentDescription = null,
                        modifier = Modifier
                            .size(width = 100.dp, height = 70.dp)
                    )
                    Text(
                        text = "Correct",
                        textAlign = TextAlign.Center,
                        maxLines = 1,
                        fontSize = 10.sp,

                        )

                }
            }
        }
        //底部确定按钮
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.BottomCenter
        ) {
            Button(
                onClick = { },
//                enabled = LoginViewodel.RadioButtonValue,
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xffFFCC00),
                    contentColor = Color.White
                ),
                modifier = Modifier.fillMaxWidth(),
                shape = RectangleShape //设置形状为矩形
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Text(
                        text = "CONFIRM",
                        modifier = Modifier.weight(1f),
                        textAlign = TextAlign.Center,
                        fontSize = 20.5.sp,
                    )
                }
            }
        }




    }



}


@Preview
@Composable
fun OcrCardScreenScreenPreview() {
    val navController = rememberNavController() // 假设这里使用了导航控制器
    OcrCardScreen(navController)
}
